# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'windows.ui'
#
# Created: Sat Oct  1 22:44:32 2011
#      by: PyQt4 UI code generator 4.7.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from Matrix import Matrix
import time

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(602, 452)
  #      self.matrix=Matrix()
#        self.frame = QtGui.QFrame(Form)
        self.frame=Board(Form)
        self.frame.setFrameStyle(QtGui.QFrame.Sunken)
        self.frame.setGeometry(QtCore.QRect(1, 1, 450, 450))
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.widget = QtGui.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(460, 240, 111, 201))
        self.widget.setObjectName("widget")
        self.verticalLayout = QtGui.QVBoxLayout(self.widget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.lcdNumber = QtGui.QLCDNumber(self.widget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lcdNumber.sizePolicy().hasHeightForWidth())
        self.lcdNumber.setSizePolicy(sizePolicy)
        self.lcdNumber.setObjectName("lcdNumber")

        QtCore.QObject.connect(self.frame,QtCore.SIGNAL("scorechanged(int)"),self.Display)

        self.verticalLayout.addWidget(self.lcdNumber)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.Start = QtGui.QPushButton(self.widget)
        self.Start.setObjectName("Start")
        self.verticalLayout.addWidget(self.Start)
        QtCore.QObject.connect(self.Start,QtCore.SIGNAL("clicked()"),self.frame.Init)

        self.quit = QtGui.QPushButton(self.widget)
        self.quit.setObjectName("quit")
        self.verticalLayout.addWidget(self.quit)

        QtCore.QObject.connect(self.quit,QtCore.SIGNAL("clicked()"),Form.close)

        self.about = QtGui.QPushButton(self.widget)
        self.about.setObjectName("about")
        self.verticalLayout.addWidget(self.about)
        self.widget1 = QtGui.QWidget(Form)
        self.widget1.setGeometry(QtCore.QRect(460, 110, 121, 81))
        self.widget1.setObjectName("widget1")
        self.Next = QtGui.QLabel(Form)
        self.Next.setGeometry(QtCore.QRect(460, 70, 111, 31))
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.Next.sizePolicy().hasHeightForWidth())
        self.Next.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setWeight(75)
        font.setBold(True)
        self.Next.setFont(font)
        self.Next.setObjectName("Next")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "彩球连线", None, QtGui.QApplication.UnicodeUTF8))
        self.Start.setText(QtGui.QApplication.translate("Form", "Start", None, QtGui.QApplication.UnicodeUTF8))
        self.quit.setText(QtGui.QApplication.translate("Form", "quit", None, QtGui.QApplication.UnicodeUTF8))
        self.about.setText(QtGui.QApplication.translate("Form", "About", None, QtGui.QApplication.UnicodeUTF8))
        self.Next.setText(QtGui.QApplication.translate("Form", "Next colors", None, QtGui.QApplication.UnicodeUTF8))

    def Display(self,number):
        self.lcdNumber.display(number)

class Board(QtGui.QFrame):
    def __init__(self,parent):
        QtGui.QFrame.__init__(self, parent)
        self.setFrameStyle(QtGui.QFrame.Sunken)
        self.setGeometry(QtCore.QRect(1, 1, 450, 450))
        self.setFrameShape(QtGui.QFrame.StyledPanel)
        self.setFrameShadow(QtGui.QFrame.Raised)
        self.setObjectName("frame")
        self.matrix=Matrix()
        self.FIRST_CLICK=False
        self.SECOND_CLICK=False
        self.X1=0
        self.Y1=0
        self.X2=0
        self.Y2=0
        self.timer = QtCore.QTimer(self)
        self.pixmap=list()
        self.pixmap.append(QtGui.QPixmap(":/images/11.png"))
        self.pixmap.append(QtGui.QPixmap(":/images/22.png"))
        self.pixmap.append(QtGui.QPixmap(":/images/33.png"))
        self.pixmap.append(QtGui.QPixmap(":/images/44.png"))
        self.pixmap.append(QtGui.QPixmap(":/images/55.png"))
        self.pixmap.append(QtGui.QPixmap(":/images/66.png"))
        self.pixmap.append(QtGui.QPixmap(":/images/77.png"))

    def Init(self):
        self.matrix=Matrix()
        self.FIRST_CLICK=False
        self.SECOND_CLICK=False
        self.update()

    def paintEvent(self,event):
        painter=QtGui.QPainter(self)
        pen=QtGui.QPen(self)
        pen.setWidth(2)
        pen.setColor(QtGui.QColor(255,0,0,127))
     #   pen.setStyle(Solidline)
        painter.setPen(pen)
        for i in range(9):
            painter.drawLine(50*(i+1),0,50*(i+1),450)
            painter.drawLine(0,50*(i+1),450,50*(i+1))
        

        for i in range(9):
            for j in range(9):
                num=self.matrix.Main_matrix[i,j]
                if(num!=0):
                    painter.drawPixmap(i*50+7,j*50+7,36,36,self.pixmap[num-1])

    def mousePressEvent(self,event):
        self.update()
#        print self.matrix.Main_matrix
        pos=event.pos()
        x=pos.x()/50
        y=pos.y()/50
    #    print x,y
        if self.matrix.Main_matrix[x,y]!=0 and self.FIRST_CLICK==False:
            self.FIRST_CLICK=True
            self.X1=x
            self.Y1=y
   #         print self.FIRST_CLICK
        if self.matrix.Main_matrix[x,y]==0 and self.FIRST_CLICK==True: 
            self.SECOND_CLICK=True
            self.X2=x
            self.Y2=y
    #        print "SECOND_CLICK true"

        if self.FIRST_CLICK==True and self.SECOND_CLICK==True:
#            print self.X1,self.Y1
#            print self.X2,self.Y2
            trace=self.matrix.move(self.X1,self.Y1,self.X2,self.Y2)
            if type(trace)!=bool:
       #         print "move from (%d,%d) to (%d,%d)" %(self.X1,self.Y1,self.X2,self.Y2)
                color=self.matrix.Main_matrix[self.X1,self.Y1]
     #           print color
                self.matrix.Main_matrix[self.X1,self.Y1]=0
                self.matrix.Main_matrix[self.X2,self.Y2]=color
                self.update()
        #        print self.matrix.Main_matrix
    #            print trace
               # for i in range(trace.shape[1]):
                    #     self.matrix.Main_matrix[trace[0,i],trace[1,i]]=color
                    #     QtCore.QTimer.singleShot(3000,self.update)
                  #  self.matrix.Main_matrix[trace[0,i],trace[1,i]]=color
                    #print self.matrix.Main_matrix
                  #  self.update()
                  #  time.sleep(0.1)
                #    if i != trace.shape[1]-1:
                #        self.matrix.Main_matrix[trace[0,i],trace[1,i]]=0


                self.FIRST_CLICK=False
                self.SECOND_CLICK=False

         #       print "checking lines"
                self.matrix.Check()
                self.emit(QtCore.SIGNAL("scorechanged(int)"),self.matrix.score)
          #      print "the result after check."
          #      print self.matrix.Main_matrix
                self.update()
                if self.matrix.get_space()>3:
                    self.matrix.update()
           #         print "result after add bolls."
           #         print self.matrix.Main_matrix
                    self.matrix.Check()
                    self.update()
           #         print "result after last check"
          #          print self.matrix.Main_matrix
                else:
                    reply = QtGui.QMessageBox.question(self, 'Message',
                            "Game finished.\n Are you want to play again?", QtGui.QMessageBox.Yes |
                            QtGui.QMessageBox.No, QtGui.QMessageBox.No)
                    if reply == QtGui.QMessageBox.Yes:
                        self.Init()
                    else:
                        pass

                    




def About_dialog():
    pass

